#encoding:utf-8
from app import db
from passlib.apps import custom_app_context as pwd_context
from app.common.common import random_str
from datetime import datetime
from app.models.cusotmer import *

#商户
class Shop_list(db.Model):
    __tablename__ = 'shop_list'
    userName = db.Column(db.BigInteger,primary_key = True)
    passWord = db.Column(db.Text,nullable=False)
    good_classs = db.relationship('Goods_class',backref=db.backref('shop'))
    def hash_password(self, password):
        self.passWord = pwd_context.encrypt(password)
    def verify_password(self, password):
        return pwd_context.verify(password, self.passWord)

#商户信息表
class Shop_info(db.Model):
    __tablename__ = 'shop_info'
    id = db.Column(db.Integer,primary_key = True,autoincrement=True)
    avatar = db.Column(db.Text,default='/static/img/moren_tx.jpg')
    name = db.Column(db.String(32),default='平台商户')
    bulletin = db.Column(db.Text,default='暂无公报') #公报
    deliveryPrice = db.Column(db.Integer,default='200')
    minPrice = db.Column(db.Integer,default='20')
    sellCount = db.Column(db.Integer,default='0')
    deliveryTime = db.Column(db.Integer,default='20') #配送时间 分钟    
    openId = db.Column(db.Text)
    shop_id = db.Column(db.BigInteger,db.ForeignKey('shop_list.userName'))
    shop_user = db.relationship('Shop_list',backref=db.backref('shop_info'),uselist=False)


#商品分类表
class Goods_class(db.Model):
    __tablename__ = 'goods_class'
    id = db.Column(db.Integer,primary_key = True,autoincrement=True)
    name = db.Column(db.String(32),nullable=False)
    type = db.Column(db.Integer,default=-1)
    shop_id = db.Column(db.BigInteger,db.ForeignKey('shop_list.userName'))
    goods = db.relationship('Goods',backref=db.backref('good_class'))

#商户商品表
class Goods(db.Model):
    __tablename__ = 'goods'
    id = db.Column(db.Integer,primary_key = True,autoincrement=True)
    name = db.Column(db.Text,nullable=False)
    Icon = db.Column(db.Text,nullable=False)
    oldPrice = db.Column(db.Integer,nullable = False)
    price = db.Column(db.Integer,nullable = False)
    description = db.Column(db.Text,default='暂无描述')
    sellCount = db.Column(db.Integer,default=0)
    class_id = db.Column(db.Integer,db.ForeignKey('goods_class.id'))

#订单表
class Order_list(db.Model):
    __tablename__ = 'order_list'
    orderNo = db.Column(db.String(50),primary_key = True) #订单号
    orderStatus = db.Column(db.Integer,default=0) # 订单状态 0待支付 1待发货 2待收货 3已完成 待评价 4退款收货
    goodsMoney = db.Column(db.BigInteger) #商品总金额
    realTotalMoney = db.Column(db.BigInteger) #实际订单总金额  进行各种折扣之后的金额
    payType = db.Column(db.Integer) #支付方式  0:货到付款 1:在线支付
    payFrom = db.Column(db.Integer) #支付来源  1:支付宝，2：微信
    isPay = db.Column(db.Integer,default=0) #是否支付  0:未支付 1:已支付
    userName = db.Column(db.String(20)) #收货人名称
    userAddress = db.Column(db.String(255)) #收件人地址
    userPhone = db.Column(db.String(20)) #收件人手机
    orderRemarks = db.Column(db.String(255),default='暂无备注') #订单备注
    isRefund = db.Column(db.Integer,default=0) #是否退款 0:否 1：是
    isAppraise = db.Column(db.Integer,default=0) #是否点评  0:未点评 1:已点评
    isClosed = db.Column(db.Integer,default=0) #是否订单已完结  0：未完结 1:已完结
    orderunique = db.Column(db.String(50),default=random_str(32)) #订单流水号
    tradeNo = db.Column(db.String(100)) #在线支付交易流水
    createTime = db.Column(db.DateTime,default=datetime.now()) #下单时间
    areaIdPath = db.Column(db.String(255)) #区域Id路径 省级id_市级id_县级Id_ 例如:110000_110100_110101_
    shopId = db.Column(db.BigInteger,db.ForeignKey('shop_list.userName')) #商户id
    staffId = db.Column(db.BigInteger,db.ForeignKey('staff.userId')) #顾客id
    orderGoods = db.relationship('Order_goods',backref=db.backref('order'))

#订单商品列表
class Order_goods(db.Model):
    id = db.Column(db.Integer,primary_key = True,autoincrement=True)
    good_id = db.Column(db.Integer) #商品id
    good_price = db.Column(db.Integer) #商品单价 分
    count = db.Column(db.Integer) #商品数量
    good_name = db.Column(db.String(50)) #商品名称
    Icon = db.Column(db.Text,nullable=False) #商品图片
    createTime = db.Column(db.DateTime, default=datetime.now()) #创建时间
    orderNo = db.Column(db.String(50),db.ForeignKey('order_list.orderNo'))


#公共商品表
class Goods_list(db.Model):
    __tablename__ = 'goods_list'
    id = db.Column(db.Integer,primary_key = True,autoincrement=True)
    name = db.Column(db.Text,nullable=False)
    Icon = db.Column(db.Text,nullable=False)
    oldPrice = db.Column(db.Integer,nullable = False)
    price = db.Column(db.Integer,nullable = False)
    description = db.Column(db.Text,default='暂无描述')
    sellCount = db.Column(db.Integer,default=0)






